package com.atguigu.ssyx.handler;

import com.atguigu.ssyx.exception.SsyxException;
import com.atguigu.ssyx.result.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Description: 全局异常处理
 * @Author: Ian
 * @Date: 2023/9/24 20:55
 */
// @RestControllerAdvice: 等于@ControllerAdvice + @ResponseBody
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理所有异常
     * @param exception
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Result error(Exception exception){
        exception.printStackTrace();;
        return Result.fail(null);
    }

    /**
     * 处理手动抛出的自定义异常
     * @param exception
     * @return
     */
    @ExceptionHandler(SsyxException.class)
    public Result error(SsyxException exception){
        exception.printStackTrace();;
        return Result.fail(null);
    }
}
